import ctypes
from tkinter import Tk, filedialog 
from pathlib import Path  

# def error_disp(func):
#     '''错误处理'''
#     def wrapper(*args, **kwargs):
#         try:
#             return func(*args, **kwargs)
#         except: pass
#     return wrapper
    
class SelectPath:
    '''这是一个可视化选择文件的类'''
    def __init__(self):
        ctypes.windll.shcore.SetProcessDpiAwareness(1)   #高dpi适配
        self.root = Tk()
        self.root.withdraw()
        self.root.wm_attributes('-topmost', 1)   #置顶显示
        self.root.update()        
    
    # @error_disp               
    def select_folder(self):
        '''选择文件夹,并返回路径'''
        path_folder = filedialog.askdirectory(title="选择文件夹")
        return path_folder
    # @error_disp  
    def select_file(self,file_type:str):
        '''选择文件,并返回路径
        参数1:file_type 文件类型'''
        path_file = filedialog.askopenfilename(title="选择文件夹",filetypes=[("图片文件", f"*{file_type}")])
        return path_file
    # @error_disp  
    def select_save_file(self,file_path:str):
        '''选择文件保存路径,并返回路径
        参数1:file_name 保存文件的名称'''
        file_name_suffix = Path(file_path)             # 获取带后缀的文件名,例如.txt
        file_name = file_name_suffix.with_suffix("").name   # 去掉后缀
        print(file_name)

        save_path = filedialog.asksaveasfilename(title="保存文件",
                                                filetypes=[("xbm files", "*.xbm"), ("All files", "*.*")],
                                                initialfile=f"{file_name}",
                                                defaultextension=".xbm"
                                                ) 
        return save_path

    
if __name__ == '__main__':
    pass

    



